<?php
namespace app\admin\controller;

use Qiniu\Auth;
use app\common\logic\Logic;
use app\common\model\Car as CarModel;

class Car extends Base
{
    function _initialize()
    {
        parent::_initialize();
        $this->assign('stamp',123);
    }

    public function index()
    {
        $where = [];
        if(!empty($_GET['car_name'])){
            $where['car_name'] = ['like','%'.trim(input('get.car_name')).'%'];
        }
        if(is_numeric($_GET['type'])){
            $where['type'] = input('get.type');
        }
        if(is_numeric($_GET['status'])){
            $where['status'] = input('get.status');
        }
        $model = new CarModel();
        $rows = $model->getList($where);
        $this->assign([
            'title' => '座驾列表',
            'pageHTML' => $rows->render(),
            'rows' => $rows
        ]);
        return view();
    }

    public function edit()
    {
        if(request()->isPost()){
            $data   = input('post.');
            $model  = new Logic();
            $result = $model->saveChange('car',$data,'car');
            if($result !== false){
                $this->success('操作成功',url('index'));
            }
            $this->error($model->getError());
        }
        $domain = config('qiniu')['domain'];
        $auth  = new Auth(config('qiniu')['ACCESSKEY'], config('qiniu')['SECRETKEY']);
        $token = $auth->uploadToken(config('qiniu')['bucket']);
        $this->_show('car','car_id','','',[]);
        $this->assign([
            'title' => '座驾编辑',
            'token' => $token,
            'domain' => $domain,
        ]);
        return view();
    }

    public function change()
    {
        $this->_change('car');
    }
}
